/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.workflow.logic.operations;

import java.lang.reflect.Method;

import org.activequant.workflow.logic.annotations.Process;
import org.apache.log4j.Logger;


/**
 * An OperationBase subclass. Holds the following associated variables:
 * <ul>
 * <li>service(Object)</li>
 * <li>methods(Method[])</li>
 * </ul>
 * <br>
 * <b>History:</b><br>
 *  - [07.03.2007] Created (Erik Nijkamp)<br>
 *
 *  @author Erik Nijkamp
 */
public class Step extends OperationBase {
	
	protected final static Logger log = Logger.getLogger(Step.class);
	/**
	 * private Method[] methods;
	 */
	private Method[] methods;
	/**
	 * private Object service;
	 */
	private Object service;
	/**
	 * constructs a Step(OperationBase subclass) using the given service(Object) to set its associated service(Object).<br/>
	 * Sets its associated methods(Method[]) with all Process annotated methods declared in the Class of the given service(Object).
	 * @param service
	 */
	public Step(Object service) {
		this.methods = getMethodsByAnnotation(service);
		this.service = service;
	}
	/**
	 * constructs a Step(OperationBase subclass) using the given service(Object) to set its associated service(Object).<br/>
	 * Sets its associated methods(Method[]) with the methods whose name matches the given function(String) declared in the Class of the given service(Object).
	 * @param service
	 * @param function
	 */
	public Step(Object service, String function) {
		this.service = service;
		this.methods = getMethodsByName(service, function);
	}
	/**
	 * returns a Method[] holding all Process annotated methods declared(and inherited) in the Class of the given target(Object)
	 * @param target
	 * @return
	 */
	private Method[] getMethodsByAnnotation(Object target) {
		return getMethodsByAnnotation(target.getClass(), Process.class);
	}
	/**
	 * finds the Method which takes no arguments from the associated methods(Method[]) and then invokes that Method on the
	 * associated service(Object). Returns a Result object holding the result of the invocation as its data(Object).
	 * as its operation(IOperation).
	 */
	public Result execute() throws Exception {
		return execute(new Object[]{});
	}
	/**
	 * finds the Method whose signature accepts the given arg(Object) from the associated methods(Method[]) and then invokes that Method on the
	 * associated service(Object) passing in the given arg(Object). Returns a Result object holding the result of the invocation as its data(Object).
	 * as its operation(IOperation).
	 */
	public Result execute(Object arg) throws Exception {
		return execute(new Object[]{arg});
	}
	/**
	 * finds the Method whose signature accepts the given args(Object[]) from the associated methods(Method[]) and then invokes that Method on the
	 * associated service(Object) passing in the given args(Object[]). Returns a Result object holding the result of the invocation as its data(Object).
	 * as its operation(IOperation).
	 * @param args
	 * @return
	 * @throws Exception
	 */
	private Result execute(Object[] args) throws Exception {
		Method method = getMethodBySignature(methods, args);
		log.info("Invoking method '" + method.getName()
				+ "' in service class '" + service.getClass().getSimpleName()
				+ "'.");
		return new Result(method.invoke(service, args));
	}
	/**
	 * finds the Method whose return type matches the given signature(Class&lt;?&gt;) from the associated methods(Method[]) and then invokes that Method on the
	 * associated service(Object) passing in the given args(Object[]). Returns a Result object holding the result of the invocation as its data(Object).
	 */
	public Result execute(Class<?> signature) throws Exception {
		Method method = getMethodByReturnType(methods, signature);
		log.info("Invoking method '" + method.getName()
				+ "' in service class '" + service.getClass().getSimpleName()
				+ "'.");
		return new Result(method.invoke(service, new Object[]{}));
	}
	/**
	 * returns the associated methods(Method[])
	 */
	public Method[] getMethods() {
		return methods;
	}
}
